//
//  cacheView.m
//  基本框架
//
//  Created by runone02 on 2018/7/30.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "cacheView.h"
#import "XHNetworkCache.h"
@implementation cacheView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.saveBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    self.saveBtn.titleLabel.font = TITLE_FONT(30);
    [self.saveBtn setTitle:@"缓存数据(同步)" forState:UIControlStateNormal];
    
    [self.saveBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.saveBtn.layer.masksToBounds = YES;
    self.saveBtn.layer.borderWidth = 1.0f;
    self.saveBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.saveBtn.layer.cornerRadius = W_RATIO(10);
    [self.saveBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.saveBtn];
    [self.saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).with.offset(kTopBarHeight + 20);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(50);
    }];
    
    self.saveAsyncBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    self.saveAsyncBtn.titleLabel.font = TITLE_FONT(30);
    [self.saveAsyncBtn setTitle:@"缓存数据(异步)" forState:UIControlStateNormal];
    
    [self.saveAsyncBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.saveAsyncBtn.layer.masksToBounds = YES;
    self.saveAsyncBtn.layer.borderWidth = 1.0f;
    self.saveAsyncBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.saveAsyncBtn.layer.cornerRadius = W_RATIO(10);
    [self.saveAsyncBtn addTarget:self action:@selector(saveAsyncBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.saveAsyncBtn];
    [self.saveAsyncBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.saveBtn.mas_bottom).with.offset(20);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(50);
    }];
    
    self.getCacheBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    self.getCacheBtn.titleLabel.font = TITLE_FONT(30);
    [self.getCacheBtn setTitle:@"获取数据" forState:UIControlStateNormal];
    [self.getCacheBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.getCacheBtn.layer.masksToBounds = YES;
    self.getCacheBtn.layer.borderWidth = 1.0f;
    self.getCacheBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.getCacheBtn.layer.cornerRadius = W_RATIO(10);
    [self.getCacheBtn addTarget:self action:@selector(getCacheBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.getCacheBtn];
    [self.getCacheBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.saveAsyncBtn.mas_bottom).with.offset(20);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(50);
    }];
    
    self.cacheSizeBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    self.cacheSizeBtn.titleLabel.font = TITLE_FONT(30);
    [self.cacheSizeBtn setTitle:@"获取数据大小" forState:UIControlStateNormal];
    [self.cacheSizeBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.cacheSizeBtn.layer.masksToBounds = YES;
    self.cacheSizeBtn.layer.borderWidth = 1.0f;
    self.cacheSizeBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.cacheSizeBtn.layer.cornerRadius = W_RATIO(10);
    [self.cacheSizeBtn addTarget:self action:@selector(cacheSizeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.cacheSizeBtn];
    [self.cacheSizeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.getCacheBtn.mas_bottom).with.offset(20);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(50);
    }];
    
    self.cachePathBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    self.cachePathBtn.titleLabel.font = TITLE_FONT(30);
    [self.cachePathBtn setTitle:@"获取路径" forState:UIControlStateNormal];
    [self.cachePathBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.cachePathBtn.layer.masksToBounds = YES;
    self.cachePathBtn.layer.borderWidth = 1.0f;
    self.cachePathBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.cachePathBtn.layer.cornerRadius = W_RATIO(10);
    [self.cachePathBtn addTarget:self action:@selector(cachePathBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.cachePathBtn];
    [self.cachePathBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.cacheSizeBtn.mas_bottom).with.offset(20);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(50);
    }];
    
    self.clearCacheBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    self.clearCacheBtn.titleLabel.font = TITLE_FONT(30);
    [self.clearCacheBtn setTitle:@"清理缓存" forState:UIControlStateNormal];
    [self.clearCacheBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.clearCacheBtn.layer.masksToBounds = YES;
    self.clearCacheBtn.layer.borderWidth = 1.0f;
    self.clearCacheBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.clearCacheBtn.layer.cornerRadius = W_RATIO(10);
    [self.clearCacheBtn addTarget:self action:@selector(clearCacheBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.clearCacheBtn];
    [self.clearCacheBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.cachePathBtn.mas_bottom).with.offset(20);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(50);
    }];
}

- (void)saveBtnClick:(UIButton *)btn{
    NSLog(@"同步存储");
}

- (void)saveAsyncBtnClick:(UIButton*)btn{
    NSLog(@"异步缓存");
}

- (void)getCacheBtnClick:(UIButton *)btn{
    NSLog(@"获取数据");
}

- (void)cacheSizeBtnClick:(UIButton *)btn{
    float size = [XHNetworkCache cacheSize];
    NSLog(@"缓存大小%fM",size);
    
    NSString *sizeFormat = [XHNetworkCache cacheSizeFormat];
    NSLog(@"缓存大小%@",sizeFormat);
}

- (void)cachePathBtnClick:(UIButton *)btn{
    NSString *path = [XHNetworkCache cachePath];
    NSLog(@"缓存路径:%@",path);
}

- (void)clearCacheBtnClick:(UIButton *)btn{
    
    BOOL result = [XHNetworkCache clearCache];
    if (result) {
        NSLog(@"清理缓存成功");
    }
    
}
@end
